# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import sys

from PySide2.QtCore import Signal, QObject
from PySide2.QtGui import QCloseEvent, QTextCursor
from PySide2.QtWidgets import QApplication

from ui_component.ui_main.ui_main import Application, Main_Ui

import warnings

warnings.filterwarnings("ignore")


class Stream(QObject):
    conn = True
    newText = Signal(str)

    def write(self, text):
        if not self.conn:
            return
        self.newText.emit(str(text))
        # 实时刷新界面
        QApplication.processEvents()


class main_ui(Main_Ui):

    def __init__(self, parent=None, license_control=False):
        super(main_ui, self).__init__(parent=parent, license_control=license_control)
        self.sd = Stream()
        self.se = Stream()
        self.sd.newText.connect(self.outputWritten)
        self.se.newText.connect(self.outputWritten)
        sys.stdout = self.sd
        sys.stderr = self.se

    def outputWritten(self, text):
        cursor = self.text_browser.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.text_browser.setTextCursor(cursor)
        self.text_browser.ensureCursorVisible()

    def closeEvent(self, a0: QCloseEvent) -> None:
        sys.stdout.conn = False
        sys.stderr.conn = False
        sys.exit(0)


# pyinstaller -F -w -i icon_swf.ico --add-data workspace_core\stdf_core\stdf_to_csv\stdf_ctype.dll;. --add-data workspace_core\stdf_core\csv_to_stdf\csv_to_stdf.dll;. app.py
if __name__ == '__main__':
    """
    * 865789047@qq.com *
    *       Link       *
    *       V0.1       *
    """
    import multiprocessing

    multiprocessing.freeze_support()

    app = Application(sys.argv)
    app.setApplicationName("IC DATA ANALYSIS")
    win = main_ui(license_control=False)
    app.setWindowIcon(win.icon)
    win.show()
    sys.exit(app.exec_())
